<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>老油条 | 老油条</title>
    <meta name="description" content="个人的成长之路，包含前端常用知识、源码阅读笔记、各种奇淫技巧、日常提效工具等">
    <link rel="preload stylesheet" href="/assets/style.78397f57.css" as="style">
    <link rel="modulepreload" href="/assets/app.d59c877d.js">
    <link rel="modulepreload" href="/assets/document_java_Stream详解.md.04298379.lean.js">
    
    <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/favicon.ico">
  <link rel="mask-icon" href="/favicon.ico" color="#3eaf7c">
  <meta name="msapplication-TileImage" content="/favicon.ico">
  <meta name="msapplication-TileColor" content="#000000">
  <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-37f477df><!--[--><!--]--><!--[--><span tabindex="-1" data-v-4fc2cfce></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-4fc2cfce> Skip to content </a><!--]--><!----><header class="VPNav" data-v-37f477df data-v-8263d7e6><div class="VPNavBar has-sidebar" data-v-8263d7e6 data-v-25a29d8b><div class="container" data-v-25a29d8b><div class="title" data-v-25a29d8b><div class="VPNavBarTitle has-sidebar" data-v-25a29d8b data-v-bbc64f2f><a class="title" href="/" data-v-bbc64f2f><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-7770c775><!--]--><!--[-->老油条<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-25a29d8b><div class="curtain" data-v-25a29d8b></div><div class="content-body" data-v-25a29d8b><!--[--><!--]--><!----><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-25a29d8b data-v-349f684d><span id="main-nav-aria-label" class="visually-hidden" data-v-349f684d>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->首页<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/document/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->个人文档<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nav/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->网站导航<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/video/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->剪影教程<!--]--><!----></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-25a29d8b data-v-8c0bdc46><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" aria-label="toggle dark mode" aria-checked="false" data-v-8c0bdc46 data-v-d714f3df data-v-93e21d5e><span class="check" data-v-93e21d5e><span class="icon" data-v-93e21d5e><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-d714f3df><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-d714f3df><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><!----><div class="VPFlyout VPNavBarExtra extra" data-v-25a29d8b data-v-6fc9b924 data-v-b1f35f78><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-b1f35f78><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-b1f35f78><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-b1f35f78><div class="VPMenu" data-v-b1f35f78 data-v-21275a90><!----><!--[--><!--[--><!----><div class="group" data-v-6fc9b924><div class="item appearance" data-v-6fc9b924><p class="label" data-v-6fc9b924>外观</p><div class="appearance-action" data-v-6fc9b924><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" aria-label="toggle dark mode" aria-checked="false" data-v-6fc9b924 data-v-d714f3df data-v-93e21d5e><span class="check" data-v-93e21d5e><span class="icon" data-v-93e21d5e><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-d714f3df><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-d714f3df><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><!----><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-25a29d8b data-v-89c94d3e><span class="container" data-v-89c94d3e><span class="top" data-v-89c94d3e></span><span class="middle" data-v-89c94d3e></span><span class="bottom" data-v-89c94d3e></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav" data-v-37f477df data-v-c87b4d81><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-c87b4d81><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-c87b4d81><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-c87b4d81>Menu</span></button><a class="top-link" href="#" data-v-c87b4d81>返回顶部</a></div><aside class="VPSidebar" data-v-37f477df data-v-47b3bdca><div class="curtain" data-v-47b3bdca></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-47b3bdca><span class="visually-hidden" id="sidebar-aria-label" data-v-47b3bdca> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-47b3bdca><section class="VPSidebarItem level-0" data-v-47b3bdca data-v-1d351fe6><div class="item" role="button" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link" data-v-1d351fe6 data-v-f98ca989><!--[--><h2 class="text" data-v-1d351fe6>个人文档</h2><!--]--><!----></a><!----></div><div class="items" data-v-1d351fe6><!--[--><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>简介</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/video/dir" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>目录</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-47b3bdca><section class="VPSidebarItem level-0 has-active" data-v-47b3bdca data-v-1d351fe6><div class="item" role="button" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link" data-v-1d351fe6 data-v-f98ca989><!--[--><h2 class="text" data-v-1d351fe6>java</h2><!--]--><!----></a><!----></div><div class="items" data-v-1d351fe6><!--[--><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/java/Java%20%E9%9B%86%E5%90%88" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>Java集合 </p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link is-active has-active" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/java/Stream%E8%AF%A6%E8%A7%A3" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>Stream详解</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-47b3bdca><section class="VPSidebarItem level-0" data-v-47b3bdca data-v-1d351fe6><div class="item" role="button" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link" data-v-1d351fe6 data-v-f98ca989><!--[--><h2 class="text" data-v-1d351fe6>SpringBoot</h2><!--]--><!----></a><!----></div><div class="items" data-v-1d351fe6><!--[--><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/java/SpringBoot%20@Async%E7%AE%80%E4%BB%8B" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>注解@Async简介</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-37f477df data-v-c336b2e4><div class="VPDoc has-sidebar has-aside" data-v-c336b2e4 data-v-7f422fda><div class="container" data-v-7f422fda><div class="aside" data-v-7f422fda><div class="aside-curtain" data-v-7f422fda></div><div class="aside-container" data-v-7f422fda><div class="aside-content" data-v-7f422fda><div class="VPDocAside" data-v-7f422fda data-v-53f738a8><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-53f738a8 data-v-5687e8fd><div class="content" data-v-5687e8fd><div class="outline-marker" data-v-5687e8fd></div><div class="outline-title" data-v-5687e8fd>本页目录</div><nav aria-labelledby="doc-outline-aria-label" data-v-5687e8fd><span class="visually-hidden" id="doc-outline-aria-label" data-v-5687e8fd> Table of Contents for current page </span><ul class="root" data-v-5687e8fd data-v-2416c4df><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-53f738a8></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-7f422fda><div class="content-container" data-v-7f422fda><!--[--><!--]--><main class="main" data-v-7f422fda><div style="position:relative;" class="vp-doc _document_java_Stream%E8%AF%A6%E8%A7%A3" data-v-7f422fda><div><h2 id="前言" tabindex="-1">前言 <a class="header-anchor" href="#前言" aria-hidden="true">#</a></h2><blockquote><p>Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本。 Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ，它支持函数式编程，新的 JavaScript 引擎，新的日期 API，新的<a href="https://so.csdn.net/so/search?q=Stream&amp;spm=1001.2101.3001.7020" target="_blank" rel="noreferrer">Stream</a> API 等。</p><p>新添加的Stream API（java.util.stream） 把真正的函数式编程风格引入到Java中。它可以让你以一种声明的方式处理数据，从而写出高效率、干净、简洁的代码。</p><p>这种风格将要处理的元素集合看作一种流， 流在管道中传输， 并且可以在管道的节点上进行处理， 比如筛选， 排序，聚合等。</p></blockquote><h2 id="一、stream-特性" tabindex="-1">一、Stream 特性 <a class="header-anchor" href="#一、stream-特性" aria-hidden="true">#</a></h2><ol><li>元素是特定类型的对象，形成一个队列。 Java中的Stream不会存储元素，而是按需计算按照特定的规则对数据进行计算，一般会输出结果。</li><li>Stream不会改变数据源，一般情况下会产生一个新的集合或者新值。</li><li>Stream流的来源，可以是集合，数组，I/O channel， 产生器generator 等等。</li><li>Stream具有延迟执行特性，只有调用终端操作时，中间操作才会执行。中间操作都会返回流对象本身。 这样多个操作可以串联成一个管道， 如同流式风格（fluent style）。</li><li>以前对集合遍历都是通过Iterator或者For-Each的方式, 显式的在集合外部进行迭代， 这叫做外部迭代。 Stream提供了内部迭代的方式， 通过访问者模式(Visitor)实现。</li></ol><hr><p>Stream流的操作可以大概分为2种：</p><ol><li><strong>中间操作</strong>：每次操作都返回流对象本身。</li><li><strong>终端操作</strong>：一个流只可以进行一次终端操作，即产生一个新的集合或者新值。终端操作结束后流无法再次使用。</li></ol><h2 id="二、stream-创建" tabindex="-1">二、Stream 创建 <a class="header-anchor" href="#二、stream-创建" aria-hidden="true">#</a></h2><blockquote><p>在 Java 8 中, Stream可以由集合或数组创建而来，生成的流有2种类型：</p><ol><li><strong>stream()</strong> ：串行流，由主线程按顺序对流执行操作。</li><li><strong>parallelStream()</strong> ：并行流，内部以多线程并行执行的方式对流进行操作，但前提是流中的数据处理没有顺序要求。例如计算集合中的数量之和。如果流种数据量很大，并行流可以加快处理速度。串行流可以通过<code>parallel()</code>方法把顺序流转换成并行流。</li></ol></blockquote><h3 id="_2-1-用集合创建流" tabindex="-1">2.1 用集合创建流 <a class="header-anchor" href="#_2-1-用集合创建流" aria-hidden="true">#</a></h3><blockquote><p>因为集合继承或实现了<code>java.util.Collection</code>接口，而Collection接口定义了<code>stream()</code>和<code>parallelStream()</code>方法，所以可通过集合的stream() 和parallelStream()方法创建流。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 创建集合</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; list </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">asList</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;王五&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#6A737D;">// 创建一个串行流</span></span>
<span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; stream </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> list.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#6A737D;">// 创建一个并行流</span></span>
<span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; parallelStream </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> list.</span><span style="color:#B392F0;">parallelStream</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_2-2-用数组创建流" tabindex="-1">2.2 用数组创建流 <a class="header-anchor" href="#_2-2-用数组创建流" aria-hidden="true">#</a></h3><blockquote><p>使用java.util.Arrays.stream(T[] array)方法用数组创建流。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 创建数组</span></span>
<span class="line"><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">[] persons </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> {</span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;王五&quot;</span><span style="color:#E1E4E8;">};</span></span>
<span class="line"><span style="color:#6A737D;">// 创建一个串行流</span></span>
<span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; stream </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">(persons);</span></span>
<span class="line"><span style="color:#6A737D;">// 创建一个并行流</span></span>
<span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; parallelStream </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">(persons).</span><span style="color:#B392F0;">parallel</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_2-3-stream静态方法" tabindex="-1">2.3 Stream静态方法 <a class="header-anchor" href="#_2-3-stream静态方法" aria-hidden="true">#</a></h3><blockquote><p>使用Stream的静态方法生成Stream，例如<code>of()</code>、<code>iterate()</code>、<code>generate()</code>等。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; stream2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Stream.</span><span style="color:#B392F0;">of</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;王五&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出5个奇数 1 3 5 7 9</span></span>
<span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; stream3 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Stream.</span><span style="color:#B392F0;">iterate</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, x </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> x </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">limit</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">stream3.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 生成2个UUID</span></span>
<span class="line"><span style="color:#E1E4E8;">Stream&lt;</span><span style="color:#F97583;">UUID</span><span style="color:#E1E4E8;">&gt; stream4 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Stream.</span><span style="color:#B392F0;">generate</span><span style="color:#E1E4E8;">(UUID</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">randomUUID).</span><span style="color:#B392F0;">limit</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">stream4.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="三、stream-使用案例" tabindex="-1">三、Stream 使用案例 <a class="header-anchor" href="#三、stream-使用案例" aria-hidden="true">#</a></h2><blockquote><p>以下所有案例会基于学生数据，学生类，以及测试数据如下：</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#F97583;">package</span><span style="color:#E1E4E8;"> com.nobody;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">/**</span></span>
<span class="line"><span style="color:#6A737D;"> * @Description 学生类</span></span>
<span class="line"><span style="color:#6A737D;"> * @Author Mr.nobody</span></span>
<span class="line"><span style="color:#6A737D;"> * @Date 2021/1/17</span></span>
<span class="line"><span style="color:#6A737D;"> * @Version 1.0</span></span>
<span class="line"><span style="color:#6A737D;"> */</span></span>
<span class="line"><span style="color:#F97583;">public</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">class</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#6A737D;">// 主键</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">private</span><span style="color:#E1E4E8;"> String id;</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#6A737D;">// 姓名</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">private</span><span style="color:#E1E4E8;"> String name;</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#6A737D;">// 年龄</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">private</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">int</span><span style="color:#E1E4E8;"> age;</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#6A737D;">// 性别</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">private</span><span style="color:#E1E4E8;"> String sex;</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#6A737D;">// 成绩</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">private</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">double</span><span style="color:#E1E4E8;"> score;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">public</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(String </span><span style="color:#FFAB70;">id</span><span style="color:#E1E4E8;">, String </span><span style="color:#FFAB70;">name</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">int</span><span style="color:#E1E4E8;"> </span><span style="color:#FFAB70;">age</span><span style="color:#E1E4E8;">, String </span><span style="color:#FFAB70;">sex</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">double</span><span style="color:#E1E4E8;"> </span><span style="color:#FFAB70;">score</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#79B8FF;">this</span><span style="color:#E1E4E8;">.id </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> id;</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#79B8FF;">this</span><span style="color:#E1E4E8;">.name </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> name;</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#79B8FF;">this</span><span style="color:#E1E4E8;">.age </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> age;</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#79B8FF;">this</span><span style="color:#E1E4E8;">.sex </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> sex;</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#79B8FF;">this</span><span style="color:#E1E4E8;">.score </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> score;</span></span>
<span class="line"><span style="color:#E1E4E8;">    }</span></span>
<span class="line"><span style="color:#E1E4E8;">	</span><span style="color:#6A737D;">// 省略get和set方法，toString方法，如若测试需自行添加</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> ArrayList&lt;&gt;(</span><span style="color:#79B8FF;">16</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;1&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">88</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;2&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">60</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;3&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;王五&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;4&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;赵六&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;5&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;董七&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;female&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">95</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;6&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;幺八&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">55</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;7&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;老九&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;female&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">66</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;8&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;小明&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;9&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;小红&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;female&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">95</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Student</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;10&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;小张&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;male&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">90</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_3-1-遍历-foreach" tabindex="-1">3.1 遍历 <a href="https://so.csdn.net/so/search?q=forEach&amp;spm=1001.2101.3001.7020" target="_blank" rel="noreferrer">forEach</a> <a class="header-anchor" href="#_3-1-遍历-foreach" aria-hidden="true">#</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;李四&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">60.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;幺八&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">55.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;10&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小张&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">90.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="_3-2-过滤-filter" tabindex="-1">3.2 过滤 filter <a class="header-anchor" href="#_3-2-过滤-filter" aria-hidden="true">#</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 过滤出成绩100分的学生</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students1 </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">==</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">students1.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_3-3-查找-findfirst-findany" tabindex="-1">3.3 查找 findFirst，findAny <a class="header-anchor" href="#_3-3-查找-findfirst-findany" aria-hidden="true">#</a></h3><blockquote><p>一般filter和find搭配使用，即从过滤符合条件的数据中，获得一个数据。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 串行流，匹配第一个</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; studentOptional </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getAge</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">&gt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">findFirst</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#F97583;">if</span><span style="color:#E1E4E8;"> (studentOptional.</span><span style="color:#B392F0;">isPresent</span><span style="color:#E1E4E8;">()) {</span></span>
<span class="line"><span style="color:#E1E4E8;">    Student student </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> studentOptional.</span><span style="color:#B392F0;">get</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(student);</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#6A737D;">// 上面输出语句可简写如下</span></span>
<span class="line"><span style="color:#6A737D;">// studentOptional.ifPresent(System.out::println);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 并行流，匹配任意一个，findAny一般用于并行流</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; studentOptiona2 </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">parallelStream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getAge</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">&gt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">findAny</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">studentOptiona2.</span><span style="color:#B392F0;">ifPresent</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h3 id="_3-4-匹配-match" tabindex="-1">3.4 匹配 match <a class="header-anchor" href="#_3-4-匹配-match" aria-hidden="true">#</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 是否存在100分的学生</span></span>
<span class="line"><span style="color:#F97583;">boolean</span><span style="color:#E1E4E8;"> anyMatch </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">anyMatch</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">==</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#6A737D;">// 是否全部学生都100分</span></span>
<span class="line"><span style="color:#F97583;">boolean</span><span style="color:#E1E4E8;"> allMatch </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">allMatch</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">==</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#6A737D;">// 是否全部学生都没有100分</span></span>
<span class="line"><span style="color:#F97583;">boolean</span><span style="color:#E1E4E8;"> noneMatch </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">noneMatch</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">==</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(anyMatch);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(allMatch);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(noneMatch);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#79B8FF;">true</span></span>
<span class="line"><span style="color:#79B8FF;">false</span></span>
<span class="line"><span style="color:#79B8FF;">false</span></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="_3-5-映射-map" tabindex="-1">3.5 映射 map <a class="header-anchor" href="#_3-5-映射-map" aria-hidden="true">#</a></h3><blockquote><p>映射，顾名思义，将一个对象映射成另外一个对象。即一个Stream流中的所有元素按照一定的映射规则，映射到另一个流中。映射有map和flatMap两种类型：</p><ol><li><strong>map</strong>：接收一个函数作为参数，此函数作用到Stream中每一个元素，形成一个新的元素，所有新的元素组成一个新的流。</li><li><strong>flatMap</strong>：接收一个函数作为参数，它将流中的每个元素都转换成另一个流，然后把所有流再连接形成一个最终流。</li></ol></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 获取每个学生的姓名</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; studentNames </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getName).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(studentNames);</span></span>
<span class="line"><span style="color:#6A737D;">// 每个学生的成绩加10分</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Double</span><span style="color:#E1E4E8;">&gt; studentScores </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(studentScores);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">[张三, 李四, 王五, 赵六, 董七, 幺八, 老九, 小明, 小红, 小张]</span></span>
<span class="line"><span style="color:#E1E4E8;">[</span><span style="color:#79B8FF;">98.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">70.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">110.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">105.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">65.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">76.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">110.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">105.0</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; list </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">asList</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;a-b-c-d&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;g-h-i&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; list1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> list.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">flatMap</span><span style="color:#E1E4E8;">(s </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">(s.</span><span style="color:#B392F0;">split</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;-&quot;</span><span style="color:#E1E4E8;">))).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(list1);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">[a, b, c, d, g, h, i]</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_3-6-截取流-limit" tabindex="-1">3.6 截取流 limit <a class="header-anchor" href="#_3-6-截取流-limit" aria-hidden="true">#</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// limit方法用于获取指定数量的流。例如下面演示取出学习成绩大于70的5个人</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">&gt;</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">70</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">limit</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">students2.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#6A737D;">// 跳过第一个再取2个</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students8 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">skip</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">limit</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#6A737D;">// 获取5个int随机数,按从小到大排序</span></span>
<span class="line"><span style="color:#E1E4E8;">Random random </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Random</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">random.</span><span style="color:#B392F0;">ints</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">limit</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">sorted</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#F97583;">-</span><span style="color:#79B8FF;">1490202714</span></span>
<span class="line"><span style="color:#79B8FF;">145340547</span></span>
<span class="line"><span style="color:#79B8FF;">368332155</span></span>
<span class="line"><span style="color:#79B8FF;">388399398</span></span>
<span class="line"><span style="color:#79B8FF;">1099579920</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_3-7-排序-sorted" tabindex="-1">3.7 排序 sorted <a class="header-anchor" href="#_3-7-排序-sorted" aria-hidden="true">#</a></h3><blockquote><p>sorted 方法用于对流中的元素进行排序，有两种排序：</p><ol><li><strong>sorted()</strong>：自然排序，流中元素需要实现Comparable接口。</li><li><strong>sorted(Comparator&lt;? super T&gt; comparator)</strong>：需要自定义排序器。</li></ol></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 按成绩升序</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students3 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">sorted</span><span style="color:#E1E4E8;">(Comparator.</span><span style="color:#B392F0;">comparing</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore))</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按成绩升序&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">students3.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#6A737D;">// 按成绩降序</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students4 </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">sorted</span><span style="color:#E1E4E8;">(Comparator.</span><span style="color:#B392F0;">comparing</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore).</span><span style="color:#B392F0;">reversed</span><span style="color:#E1E4E8;">())</span></span>
<span class="line"><span style="color:#E1E4E8;">                .</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按成绩降序&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">students4.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#6A737D;">// 按成绩升序，再按年龄升序</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students5 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">sorted</span><span style="color:#E1E4E8;">(Comparator.</span><span style="color:#B392F0;">comparing</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore).</span><span style="color:#B392F0;">thenComparing</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getAge))</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按成绩升序，再按年龄升序&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">students5.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#6A737D;">// 按成绩升序，再按年龄降序</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; students6 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">sorted</span><span style="color:#E1E4E8;">((s1, s2) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">if</span><span style="color:#E1E4E8;"> (s1.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">!=</span><span style="color:#E1E4E8;"> s2.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">()) {</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> (</span><span style="color:#F97583;">int</span><span style="color:#E1E4E8;">) (s1.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">-</span><span style="color:#E1E4E8;"> s2.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">    } </span><span style="color:#F97583;">else</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">        </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> (s2.</span><span style="color:#B392F0;">getAge</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">-</span><span style="color:#E1E4E8;"> s1.</span><span style="color:#B392F0;">getAge</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">    }</span></span>
<span class="line"><span style="color:#E1E4E8;">}).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按成绩升序，再按年龄降序&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">students6.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h3 id="_3-8-去重-distinct" tabindex="-1">3.8 去重 <a href="https://so.csdn.net/so/search?q=distinct&amp;spm=1001.2101.3001.7020" target="_blank" rel="noreferrer">distinct</a> <a class="header-anchor" href="#_3-8-去重-distinct" aria-hidden="true">#</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; list2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">asList</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;a&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;b&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;a&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;c&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;f&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;e&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;f&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; list3 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> list2.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">distinct</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">Set&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; stringSet </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> list2.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toSet</span><span style="color:#E1E4E8;">()); </span><span style="color:#6A737D;">// list转set也可以达到去重效果</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(list3);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(stringSet);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">[a, b, c, f, e]</span></span>
<span class="line"><span style="color:#E1E4E8;">[a, b, c, e, f]</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_3-9-统计-summarystatistics" tabindex="-1">3.9 统计 summaryStatistics <a class="header-anchor" href="#_3-9-统计-summarystatistics" aria-hidden="true">#</a></h3><blockquote><p>一些收集器能产生统计结果，例如Collectors提供了一系列用于数据统计的静态方法，它们主要用于int、double、long等基本类型上。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 总和统计summaryStatistics</span></span>
<span class="line"><span style="color:#E1E4E8;">DoubleSummaryStatistics doubleSummaryStatistics </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">mapToDouble</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore).</span><span style="color:#B392F0;">summaryStatistics</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;平均值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> doubleSummaryStatistics.</span><span style="color:#B392F0;">getAverage</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;总个数：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> doubleSummaryStatistics.</span><span style="color:#B392F0;">getCount</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;最大值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> doubleSummaryStatistics.</span><span style="color:#B392F0;">getMax</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;最小值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> doubleSummaryStatistics.</span><span style="color:#B392F0;">getMin</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;总和值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> doubleSummaryStatistics.</span><span style="color:#B392F0;">getSum</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">平均值：</span><span style="color:#79B8FF;">75.9</span></span>
<span class="line"><span style="color:#E1E4E8;">总个数：</span><span style="color:#79B8FF;">10</span></span>
<span class="line"><span style="color:#E1E4E8;">最大值：</span><span style="color:#79B8FF;">100.0</span></span>
<span class="line"><span style="color:#E1E4E8;">最小值：</span><span style="color:#79B8FF;">10.0</span></span>
<span class="line"><span style="color:#E1E4E8;">总和值：</span><span style="color:#79B8FF;">759.0</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 统计个数</span></span>
<span class="line"><span style="color:#F97583;">long</span><span style="color:#E1E4E8;"> count </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">count</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#6A737D;">// 平均值</span></span>
<span class="line"><span style="color:#E1E4E8;">Double averageScore </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">averagingDouble</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore));</span></span>
<span class="line"><span style="color:#6A737D;">// 最大值和最小值</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Double</span><span style="color:#E1E4E8;">&gt; maxScore </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore).</span><span style="color:#B392F0;">max</span><span style="color:#E1E4E8;">(Double</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">compare);</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Double</span><span style="color:#E1E4E8;">&gt; minScore </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore).</span><span style="color:#B392F0;">min</span><span style="color:#E1E4E8;">(Double</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">compare);</span></span>
<span class="line"><span style="color:#6A737D;">// 求和</span></span>
<span class="line"><span style="color:#F97583;">double</span><span style="color:#E1E4E8;"> sumScore </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">mapToDouble</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore).</span><span style="color:#B392F0;">sum</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#6A737D;">// 一次性统计所有</span></span>
<span class="line"><span style="color:#E1E4E8;">DoubleSummaryStatistics doubleSummaryStatistics1 </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">summarizingDouble</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getScore));</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;单个维度计算：&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;统计个数：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> count);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;平均值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> averageScore);</span></span>
<span class="line"><span style="color:#E1E4E8;">maxScore.</span><span style="color:#B392F0;">ifPresent</span><span style="color:#E1E4E8;">(aDouble </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;最大值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> aDouble));</span></span>
<span class="line"><span style="color:#E1E4E8;">minScore.</span><span style="color:#B392F0;">ifPresent</span><span style="color:#E1E4E8;">(aDouble </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;最小值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> aDouble));</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;求和：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> sumScore);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;一次性统计所有：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> doubleSummaryStatistics1);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">单个维度计算：</span></span>
<span class="line"><span style="color:#E1E4E8;">统计个数：</span><span style="color:#79B8FF;">10</span></span>
<span class="line"><span style="color:#E1E4E8;">平均值：</span><span style="color:#79B8FF;">75.9</span></span>
<span class="line"><span style="color:#E1E4E8;">最大值：</span><span style="color:#79B8FF;">100.0</span></span>
<span class="line"><span style="color:#E1E4E8;">最小值：</span><span style="color:#79B8FF;">10.0</span></span>
<span class="line"><span style="color:#E1E4E8;">求和：</span><span style="color:#79B8FF;">759.0</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h3 id="_3-10-归约-reduce" tabindex="-1">3.10 归约 reduce <a class="header-anchor" href="#_3-10-归约-reduce" aria-hidden="true">#</a></h3><blockquote><p>归约，把一个流归约(缩减)成一个值，能实现对集合求和、求乘积和求最值等操作。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; integerList </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Arrays.</span><span style="color:#B392F0;">asList</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">6</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">7</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">11</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">7</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">13</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#6A737D;">// 求和</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; sum1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> integerList.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">(Integer</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">sum);</span></span>
<span class="line"><span style="color:#6A737D;">// 求和,基于10的基础上</span></span>
<span class="line"><span style="color:#E1E4E8;">Integer sum2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> integerList.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">, Integer</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">sum);</span></span>
<span class="line"><span style="color:#6A737D;">// 求最大值</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; max1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> integerList.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">((x, y) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> x </span><span style="color:#F97583;">&gt;</span><span style="color:#E1E4E8;"> y </span><span style="color:#F97583;">?</span><span style="color:#E1E4E8;"> x </span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;"> y);</span></span>
<span class="line"><span style="color:#6A737D;">// 求最大值,基于与50比较的基础上</span></span>
<span class="line"><span style="color:#E1E4E8;">Integer max2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> integerList.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">50</span><span style="color:#E1E4E8;">, Integer</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">max);</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; min </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> integerList.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">(Integer</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">min);</span></span>
<span class="line"><span style="color:#6A737D;">// 求乘积</span></span>
<span class="line"><span style="color:#E1E4E8;">Optional&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; product </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> integerList.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">((x, y) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> x </span><span style="color:#F97583;">*</span><span style="color:#E1E4E8;"> y);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;原始集合：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> integerList);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;集合求和：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> sum1.</span><span style="color:#B392F0;">get</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&quot;,&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> sum2);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;集合求最大值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> max1.</span><span style="color:#B392F0;">get</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&quot;,&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> max2);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;集合求最小值：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> min.</span><span style="color:#B392F0;">get</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;集合求积：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> product.</span><span style="color:#B392F0;">get</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">原始集合：[</span><span style="color:#79B8FF;">6</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">7</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">11</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">7</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">13</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">集合求和：</span><span style="color:#79B8FF;">75</span><span style="color:#E1E4E8;">,</span><span style="color:#79B8FF;">85</span></span>
<span class="line"><span style="color:#E1E4E8;">集合求最大值：</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">,</span><span style="color:#79B8FF;">50</span></span>
<span class="line"><span style="color:#E1E4E8;">集合求最小值：</span><span style="color:#79B8FF;">1</span></span>
<span class="line"><span style="color:#E1E4E8;">集合求积：</span><span style="color:#79B8FF;">8408400</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h3 id="_3-11-归集-tolist-toset-tomap" tabindex="-1">3.11 归集 toList，toSet，toMap <a class="header-anchor" href="#_3-11-归集-tolist-toset-tomap" aria-hidden="true">#</a></h3><blockquote><p>Java中的Stream不会存储元素，而是按需计算按照特定的规则对数据进行计算，一般会输出结果。所以流中的数据完成处理后，需要将流中的数据重新归集到新的集合里。<code>比较常用的是toList、toSet和toMap</code>，以及复杂的toCollection、toConcurrentMap等。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 获取学生名字，形成新的list集合</span></span>
<span class="line"><span style="color:#E1E4E8;">List&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; studentNames1 </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getName).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toList</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#6A737D;">// 获取年龄大于等于15的年龄set集合</span></span>
<span class="line"><span style="color:#E1E4E8;">Set&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">&gt; ageSet </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getAge</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">&gt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">15</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getAge).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toSet</span><span style="color:#E1E4E8;">());</span></span>
<span class="line"><span style="color:#6A737D;">// 创建学生ID和学生实体的map</span></span>
<span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt; studentMap </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">toMap</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getId, student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student));</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(studentNames1);</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(ageSet);</span></span>
<span class="line"><span style="color:#E1E4E8;">studentMap.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((key, value) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(key </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&quot;:&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> value));</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">[张三, 李四, 王五, 赵六, 董七, 幺八, 老九, 小明, 小红, 小张]</span></span>
<span class="line"><span style="color:#E1E4E8;">[</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#79B8FF;">1</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">2</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;李四&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">60.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">3</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">4</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">5</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">6</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;幺八&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">55.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">7</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">8</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">9</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#79B8FF;">10</span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;10&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小张&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">90.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_3-12-分组-partitioningby-groupingby" tabindex="-1">3.12 分组 partitioningBy，groupingBy <a class="header-anchor" href="#_3-12-分组-partitioningby-groupingby" aria-hidden="true">#</a></h3><blockquote><p><strong>partitioningBy（分区）</strong>：stream中的元素按条件被分为两个Map。<br><strong>groupingBy（分组）</strong>：stream中的元素按条件被分为多个Map。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#6A737D;">// 按条件学生成绩是否大于等于60，划分为2个组</span></span>
<span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">Boolean</span><span style="color:#E1E4E8;">, List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt;&gt; studentScorePart </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">        .</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">partitioningBy</span><span style="color:#E1E4E8;">(student </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> student.</span><span style="color:#B392F0;">getScore</span><span style="color:#E1E4E8;">() </span><span style="color:#F97583;">&gt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">60</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#6A737D;">// 按性别分组</span></span>
<span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt;&gt; studentSexMap </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">groupingBy</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getSex));</span></span>
<span class="line"><span style="color:#6A737D;">// 按年龄分组</span></span>
<span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">, List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt;&gt; studentAgeMap </span><span style="color:#F97583;">=</span></span>
<span class="line"><span style="color:#E1E4E8;">        students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">groupingBy</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getAge));</span></span>
<span class="line"><span style="color:#6A737D;">// 先按性别分组，再按年龄分组</span></span>
<span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, Map&lt;</span><span style="color:#F97583;">Integer</span><span style="color:#E1E4E8;">, List&lt;</span><span style="color:#F97583;">Student</span><span style="color:#E1E4E8;">&gt;&gt;&gt; collect </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(</span></span>
<span class="line"><span style="color:#E1E4E8;">        Collectors.</span><span style="color:#B392F0;">groupingBy</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getSex, Collectors.</span><span style="color:#B392F0;">groupingBy</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getAge)));</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按条件学生成绩是否大于等于60，划分为2个组:&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">studentScorePart.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((aBoolean, students7) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;成绩大于等于60?:&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> aBoolean);</span></span>
<span class="line"><span style="color:#E1E4E8;">    students7.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#E1E4E8;">});</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按性别分组:&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">studentSexMap.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((sex, students7) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;性别?:&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> sex);</span></span>
<span class="line"><span style="color:#E1E4E8;">    students7.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#E1E4E8;">});</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;按年龄分组:&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">studentAgeMap.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((age, students7) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;年龄:&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> age);</span></span>
<span class="line"><span style="color:#E1E4E8;">    students7.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#E1E4E8;">});</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;先按性别分组，再按年龄分组:&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">collect.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((sex, integerListMap) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;性别：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> sex);</span></span>
<span class="line"><span style="color:#E1E4E8;">    integerListMap.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((age, students7) </span><span style="color:#F97583;">-&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">        System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;年龄:&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> age);</span></span>
<span class="line"><span style="color:#E1E4E8;">        students7.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">(System.out</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">println);</span></span>
<span class="line"><span style="color:#E1E4E8;">    });</span></span>
<span class="line"><span style="color:#E1E4E8;">});</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">//输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">按条件学生成绩是否大于等于60，划分为2个组</span><span style="color:#F97583;">:</span></span>
<span class="line"><span style="color:#E1E4E8;">成绩大于等于60</span><span style="color:#F97583;">?:</span><span style="color:#79B8FF;">false</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;幺八&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">55.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">成绩大于等于60</span><span style="color:#F97583;">?:</span><span style="color:#79B8FF;">true</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;李四&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">60.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;10&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小张&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">90.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">按性别分组</span><span style="color:#F97583;">:</span></span>
<span class="line"><span style="color:#E1E4E8;">性别</span><span style="color:#F97583;">?:</span><span style="color:#E1E4E8;">female</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">性别</span><span style="color:#F97583;">?:</span><span style="color:#E1E4E8;">male</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;李四&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">60.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;幺八&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">55.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;10&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小张&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">90.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">按年龄分组</span><span style="color:#F97583;">:</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">17</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;李四&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">60.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">18</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">20</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">21</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;幺八&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">55.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">22</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">25</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;10&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小张&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">90.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">14</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">先按性别分组，再按年龄分组</span><span style="color:#F97583;">:</span></span>
<span class="line"><span style="color:#E1E4E8;">性别：female</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">20</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;老九&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">66.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">22</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小红&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">22</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">14</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;董七&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">14</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">female, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">95.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">性别：male</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">17</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;李四&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">17</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">60.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">18</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;张三&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">88.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;王五&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小明&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">100.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">20</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;赵六&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">10.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">21</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;幺八&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">21</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">55.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#E1E4E8;">年龄</span><span style="color:#F97583;">:</span><span style="color:#79B8FF;">25</span></span>
<span class="line"><span style="color:#E1E4E8;">Student{id</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;10&#39;</span><span style="color:#E1E4E8;">, name</span><span style="color:#F97583;">=</span><span style="color:#9ECBFF;">&#39;小张&#39;</span><span style="color:#E1E4E8;">, age</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, sex</span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;">male, score</span><span style="color:#F97583;">=</span><span style="color:#79B8FF;">90.0</span><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br></div></div><h3 id="_3-13-合并-joining" tabindex="-1">3.13 合并 joining <a class="header-anchor" href="#_3-13-合并-joining" aria-hidden="true">#</a></h3><blockquote><p>将stream中的元素用指定的连接符（没有的话，则直接连接）连接成一个字符串。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">String joinName </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> students.</span><span style="color:#B392F0;">stream</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Student</span><span style="color:#F97583;">::</span><span style="color:#E1E4E8;">getName).</span><span style="color:#B392F0;">collect</span><span style="color:#E1E4E8;">(Collectors.</span><span style="color:#B392F0;">joining</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;, &quot;</span><span style="color:#E1E4E8;">));</span></span>
<span class="line"><span style="color:#E1E4E8;">System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(joinName);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 输出结果</span></span>
<span class="line"><span style="color:#E1E4E8;">张三, 李四, 王五, 赵六, 董七, 幺八, 老九, 小明, 小红, 小张</span></span>
<span class="line"></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></div></div></main><!--[--><!--]--><footer class="VPDocFooter" data-v-7f422fda data-v-910ca74c><!----><div class="prev-next" data-v-910ca74c><div class="pager" data-v-910ca74c><a class="pager-link prev" href="/document/java/Java%20%E9%9B%86%E5%90%88" data-v-910ca74c><span class="desc" data-v-910ca74c>上一篇</span><span class="title" data-v-910ca74c>Java集合 </span></a></div><div class="has-prev pager" data-v-910ca74c><a class="pager-link next" href="/document/java/SpringBoot%20@Async%E7%AE%80%E4%BB%8B" data-v-910ca74c><span class="desc" data-v-910ca74c>下一篇</span><span class="title" data-v-910ca74c>注解@Async简介</span></a></div></div></footer><!--[--><!--]--></div></div></div></div></div><footer class="VPFooter has-sidebar" data-v-37f477df data-v-57f271d6><div class="container" data-v-57f271d6><p class="message" data-v-57f271d6>我的征途是星辰大海,从此只顾风雨兼程,披荆斩棘。</p><p class="copyright" data-v-57f271d6>Copyright © 2019-present maomao</p></div></footer><!--[--><!--]--></div></div>
    <script>__VP_HASH_MAP__ = JSON.parse("{\"video_001_03_69.md\":\"24ee4e2a\",\"video_001_04_84.md\":\"c9ff651c\",\"video_001_04_88.md\":\"f82e3ba8\",\"video_001_03_72.md\":\"bda20a6f\",\"video_001_03_45.md\":\"4a6da3c9\",\"video_dir.md\":\"594f8a10\",\"video_001_02_25.md\":\"60b00b20\",\"video_001_02_24.md\":\"004eea6a\",\"video_001_03_49.md\":\"349159b7\",\"video_001_02_13.md\":\"4c81051c\",\"video_001_02_32.md\":\"2d2832f4\",\"video_001_02_22.md\":\"ce008882\",\"video_001_02_38.md\":\"429972e9\",\"video_001_03_79.md\":\"3d2d6110\",\"video_002_01.md\":\"23906846\",\"video_001_05_103.md\":\"6a000306\",\"video_001_02_33.md\":\"c759de41\",\"video_001_03_67.md\":\"d906792b\",\"video_001_03_53.md\":\"00e67cb9\",\"video_001_03_80.md\":\"531d3283\",\"video_001_03_57.md\":\"5839b3e3\",\"video_001_04_83.md\":\"9bd1135a\",\"video_001_03_56.md\":\"720f9f39\",\"video_001_03_52.md\":\"8bc042f6\",\"video_001_03_60.md\":\"66640c67\",\"video_001_04_97.md\":\"4f6c4b38\",\"video_001_03_62.md\":\"c45f2772\",\"video_001_02_35.md\":\"40310176\",\"video_001_04_86.md\":\"d386e4d2\",\"video_index.md\":\"53e2e028\",\"video_001_02_16.md\":\"903bc9ee\",\"video_001_04_98.md\":\"2da8a7a1\",\"video_001_01_7.md\":\"fb12b5f4\",\"video_001_02_30.md\":\"7f036e13\",\"video_001_04_99.md\":\"e6e69a97\",\"video_001_03_46.md\":\"7bef5617\",\"video_001_02_14.md\":\"10bfd666\",\"video_001_04_87.md\":\"a9af7db0\",\"video_001_02_26.md\":\"1a1531f9\",\"video_001_03_70.md\":\"63a9da24\",\"video_001_03_78.md\":\"5ae269e7\",\"video_001_02_17.md\":\"a444bd54\",\"video_001_03_51.md\":\"58bd3a1c\",\"video_001_03_43.md\":\"9d415e0b\",\"nav_index.md\":\"ba04b6ec\",\"index.md\":\"cfbd0506\",\"video_001_03_66.md\":\"c4048c68\",\"video_001_05_106.md\":\"5eadbf46\",\"video_001_03_41.md\":\"c39de5e4\",\"video_001_03_63.md\":\"424c930f\",\"video_001_02_12.md\":\"750e295c\",\"video_001_04_92.md\":\"ea0907d3\",\"video_001_04_100.md\":\"d2c2b603\",\"video_001_04_95.md\":\"0d67e61a\",\"video_001_03_77.md\":\"5cb8dc5b\",\"video_001_03_58.md\":\"6df8ce20\",\"video_001_03_44.md\":\"b1a82273\",\"video_001_05_111.md\":\"b875449f\",\"video_001_02_10.md\":\"c14f118a\",\"video_004_01.md\":\"cbd007c4\",\"document_index.md\":\"d81f3f7a\",\"video_001_03_65.md\":\"a2026925\",\"video_001_04_94.md\":\"adc3de3d\",\"video_001_01_6.md\":\"8c8b664d\",\"video_001_02_27.md\":\"5a794fcc\",\"video_001_03_74.md\":\"6a8abe02\",\"video_001_03_75.md\":\"9e72b009\",\"video_001_03_76.md\":\"b56a1116\",\"video_001_01_9.md\":\"e9638db1\",\"video_001_02_34.md\":\"c07bb664\",\"video_001_03_71.md\":\"23b34f7f\",\"video_001_02_23.md\":\"a64bac66\",\"video_001_03_40.md\":\"66a61f43\",\"video_001_01_5.md\":\"8dc2ee95\",\"video_001_02_28.md\":\"88dd7851\",\"video_001_02_31.md\":\"79e11f7c\",\"video_001_03_48.md\":\"1409b645\",\"video_001_05_105.md\":\"f285a2e7\",\"video_001_04_90.md\":\"91c1790c\",\"video_001_02_39.md\":\"7d04a723\",\"video_001_03_61.md\":\"d15404a7\",\"video_001_05_104.md\":\"bdc64109\",\"video_001_02_37.md\":\"ed548bc3\",\"video_001_02_29.md\":\"8705a744\",\"video_001_04_91.md\":\"c2e80b6c\",\"video_001_03_73.md\":\"59e59a68\",\"video_001_04_89.md\":\"bff89466\",\"video_003_01.md\":\"93b16f80\",\"video_001_02_11.md\":\"1b632398\",\"video_001_03_59.md\":\"f9fb15f1\",\"video_001_05_110.md\":\"219dd143\",\"video_001_02_36.md\":\"db5b0aea\",\"video_001_02_19.md\":\"c2182602\",\"video_001_03_68.md\":\"f92433e5\",\"video_001_04_82.md\":\"cb5d073f\",\"video_001_02_21.md\":\"680b89a5\",\"document_java_java 集合.md\":\"adfefbd7\",\"video_001_04_93.md\":\"cad2027c\",\"video_001_03_64.md\":\"7b016b05\",\"video_001_03_54.md\":\"1d5d7764\",\"video_001_05_112.md\":\"4632735a\",\"video_005_01.md\":\"6fa10e68\",\"video_001_04_96.md\":\"c87ad1e3\",\"video_001_02_15.md\":\"d3f7d717\",\"video_001_01_8.md\":\"c96a0a42\",\"video_001_05_109.md\":\"332b1d16\",\"video_001_01_4.md\":\"0a7f8b8c\",\"video_001_03_50.md\":\"18a01d2f\",\"video_001_02_18.md\":\"0a76be5f\",\"video_001_02_20.md\":\"f3b28a78\",\"video_001_03_47.md\":\"6bedeaf8\",\"document_java_springboot @async简介.md\":\"715420a9\",\"video_001_04_85.md\":\"1bea96c8\",\"video_001_05_107.md\":\"dee3f204\",\"video_001_03_55.md\":\"58004b02\",\"video_001_03_81.md\":\"7d8c84b6\",\"video_001_05_108.md\":\"92d9e16a\",\"video_001_03_42.md\":\"d3a61904\",\"video_001_05_102.md\":\"d76504b4\",\"video_001_05_101.md\":\"86e3e75d\",\"video_001_01_3.md\":\"a879eb45\",\"document_java_stream详解.md\":\"04298379\"}")</script>
    <script type="module" async src="/assets/app.d59c877d.js"></script>
    
  </body>
</html>